package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class RadioButton extends JPanel implements ActionListener {

	private JPanel sizePanel;
	private JPanel resultPanel;
	private JLabel text;
	private JRadioButton small;
	private JRadioButton medium;
	private JRadioButton large;
	private Container panelRadioBtn;
	private JLabel lblLabelBtn;
	

	public RadioButton() {
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0,0));

		lblLabelBtn = new JLabel("어떤 크기의 커피를 주문 하시겠습니까?");
		add(lblLabelBtn, BorderLayout.NORTH);
		lblLabelBtn.setHorizontalAlignment(SwingConstants.CENTER);

		text = new JLabel("크기가 선택되지 않았습니다");
		text.setHorizontalAlignment(SwingConstants.CENTER);
		text.setForeground(Color.RED);
		add(text, BorderLayout.SOUTH);

		sizePanel = new JPanel();
		add(sizePanel);
		
		Border border = BorderFactory.createTitledBorder("크기");
		sizePanel.setBorder(border);
		
		small = new JRadioButton("Small Size");
		small.addActionListener(this);
		sizePanel.add(small);

		medium = new JRadioButton("Medium Size");
		medium.addActionListener(this);
		sizePanel.add(medium);

		large = new JRadioButton("Large Size");
		large.addActionListener(this);
		sizePanel.add(large);
	
		ButtonGroup size = new ButtonGroup();
		size.add(small);
		size.add(medium);
		size.add(large);

		small.addActionListener(this);
		medium.addActionListener(this);
		large.addActionListener(this);

		sizePanel.add(small);
		sizePanel.add(medium);
		sizePanel.add(large);
		setVisible(true);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == small) {
			text.setText("Small 크기가 선택되었습니다.");
		} else if (e.getSource() == medium) {
			text.setText("Medium 크기가 선택되었습니다.");
		} else {
			text.setText("Large 크기가 선택되었습니다.");
		}
		
	}

}
